package com.cmccgz.udacs_landlord_webapp.main.warning.presenters;

import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.bean.DeviceInfoBean;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.main.warning.contracts.DevicesStatusContract;
import com.cmccgz.udacs_landlord_webapp.update.StringUtils;

import java.util.List;

import rx.Subscriber;

public class DevicesStatusPresenter implements DevicesStatusContract.Presenter {
    private DevicesStatusContract.View view;

    public DevicesStatusPresenter(DevicesStatusContract.View view) {
        this.view = view;
    }

    @Override
    public void getDevices() {
/*        ArrayList<DeviceInfoBean> devices = new ArrayList<>();
        DeviceInfoBean device_1 = new DeviceInfoBean();
        DeviceInfoBean device_2 = new DeviceInfoBean();
        DeviceInfoBean device_3 = new DeviceInfoBean();
        device_1.status="1";
        device_2.status="0";
        device_3.status="0";
        device_1.device_name="dx132";
        device_2.device_name="dx122";
        device_3.device_name="dx112";
        device_1.device_id="111";
        device_2.device_id="222";
        device_3.device_id="333";
        device_1.house_name="南州街901";
        device_2.house_name="南州街902";
        device_3.house_name="南州街903";
        devices.add(device_1);
        devices.add(device_2);
        devices.add(device_3);
        view.getDevicesSuccess(devices);*/
        ApiService.getDevicesStatus().subscribe(new Subscriber<ResultBean<List<DeviceInfoBean>>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                view.getDevicesError("获取列表失败！");
            }

            @Override
            public void onNext(ResultBean<List<DeviceInfoBean>> listResultBean) {
                if (listResultBean!=null
                        &&listResultBean.rsmsg!=null
                        &&"0".equals(listResultBean.rsmsg.rscode)
                        &&listResultBean.resp!=null) {
                    view.getDevicesSuccess(listResultBean.resp);
                }else{
                    String errorMsg=listResultBean!=null
                            &&listResultBean.rsmsg!=null
                            && !StringUtils.isEmpty(listResultBean.rsmsg.rsdesc)
                            ?listResultBean.rsmsg.rsdesc
                            :"获取列表失败！";
                    view.getDevicesError(errorMsg);
                }
            }
        });
    }
}
